package io.esirong.widget.input;

import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.TextView;

/**
 * 电话分位
 * @author 黄嵘才(HuangRc)
 */
public class PhoneFilter {

    private TextView textView;
    private static String preCharSequence;//之前的输入做对比用

    static class Filter implements InputFilter {
        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned spanned, int dstart, int dend) {
            //在onTextChanged方法里执行setText(sb.toString());会到这里，内容一样直接返回
            if (TextUtils.equals(source, preCharSequence)) {
                return null;
            }
            //过滤掉空格和换行，dstart为13表示光标位置是11位数字+2个空格时，返回空字符
            if (" ".equals(source.toString())
                    || source.toString().contentEquals("\n")
                    || dstart == 13) {
                return "";
            }
            //这里是当光标移动到非末尾位置进行输入操作时，返回空字符
            else if (getText().length() == 11) {
                return "";
            } else {
                return null;
            }
        }
    }

    private static String getText() {
        return null;
    }

    static TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
}
